//
//  MyBiaoQianViewController.m
//  1gene
//
//  Created by 1jiyin on 16/3/24.
//  Copyright © 2016年 1jiyin. All rights reserved.
//
//*********我的标签*********
#import "MyBiaoQianViewController.h"
#import "UIView+ShowPromptMessage.h"
@interface MyBiaoQianViewController ()
@property(nonatomic,copy)NSMutableArray *dataArray;
@property(nonatomic,copy)NSMutableArray *selectedArray;
/**
 *定时器
 */
@property(nonatomic,copy)NSTimer *myTimer;
@end

@implementation MyBiaoQianViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor colorWithRed:234/255.0 green:235/255.0 blue:236/255.0 alpha:1];
    [self.view addSubview:[UIView new]];
    [self initData];
    [self createNavigation];
    [self createUI];
    // Do any additional setup after loading the view.
}
-(void)initData
{
    _selectedArray=[NSMutableArray array];
    NSArray *arr=@[@"健康达人健康达人健康",@"美食达人",@"蔬菜达人",@"水果达人",@"健康达人"];
    _dataArray=[NSMutableArray arrayWithArray:arr];
}
-(void)createUI
{
    //[UIColor colorWithRed:87/255.0 green:187/255.0 blue:215/255.0 alpha:1]
    CGFloat lab_w=(self.view.frame.size.width-20*3)/2.0;
    CGFloat lab_h=30;
    NSInteger hang=[self calculatingHang:_dataArray.count];
    UIScrollView *scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 20+44, self.view.frame.size.width, self.view.frame.size.height-20-44)];
    [self.view addSubview:scrollView];
    for (int i=0; i<hang; i++) {
        for (int j=0; j<2; j++) {
            if (i*2+j<_dataArray.count) {
//            UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake(20+(lab_w+20)*j, 20+(lab_h+10)*i, lab_w, lab_h)];
//            lab.text=_dataArray[i*2+j];
//            lab.textColor=[UIColor whiteColor];
//            lab.backgroundColor=[UIColor colorWithRed:87/255.0 green:187/255.0 blue:215/255.0 alpha:1];
//            lab.layer.borderColor=[UIColor colorWithRed:87/255.0 green:187/255.0 blue:215/255.0 alpha:1].CGColor;
//            lab.layer.cornerRadius=2.0;
//            lab.layer.borderWidth=1.0;
//            lab.font=[UIFont systemFontOfSize:12];
//            lab.textAlignment=NSTextAlignmentCenter;
//            [scrollView addSubview:lab];
            
                UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
                btn.frame=CGRectMake(20+(lab_w+20)*j, 20+(lab_h+10)*i, lab_w, lab_h);
                btn.tag=i*2+j+1;
                btn.backgroundColor=[UIColor colorWithRed:87/255.0 green:187/255.0 blue:215/255.0 alpha:1];
                btn.layer.borderColor=[UIColor colorWithRed:87/255.0 green:187/255.0 blue:215/255.0 alpha:1].CGColor;
                btn.layer.cornerRadius=2.0;
                btn.layer.borderWidth=1.0;
                btn.titleLabel.font=[UIFont systemFontOfSize:12];
                [btn setTitle:_dataArray[i*2+j] forState:UIControlStateNormal];
                [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
                [scrollView addSubview:btn];

            }
        }
        scrollView.contentSize=CGSizeMake(self.view.frame.size.width, 20+(lab_h+10)*[self calculatingHang:_dataArray.count]);
    }
    
    
}
//点击标签
-(void)btnClick:(UIButton *)sender
{
    NSLog(@"..........%@",sender.titleLabel.text);
    NSArray *arr=[NSArray arrayWithArray:_selectedArray];
    NSLog(@"=========%@",arr);
    NSString *content=sender.titleLabel.text;
    BOOL contentState = false;
    for (NSString *str in arr) {
        if ([content isEqualToString:str]) {
            contentState=[content isEqualToString:str];
            break;
        }
    }

    
    
    if (_selectedArray.count==0) {
        sender.backgroundColor=[UIColor grayColor];
        [_selectedArray addObject:content];
    }else{
        if (contentState) {
            sender.backgroundColor=[UIColor colorWithRed:87/255.0 green:187/255.0 blue:215/255.0 alpha:1];
            [_selectedArray removeObject:content];
            
        }else{
            if (_selectedArray.count>=2) {
                //不操作
            }else{
                sender.backgroundColor=[UIColor grayColor];
                [_selectedArray addObject:content];
            }
        }
    }
}
-(NSInteger)calculatingHang:(NSInteger)number
{
    NSInteger hang;
    if (number%2==0) {
        hang=number/2;
    }else{
        hang=number/2+1;
    }
    return hang;
}
-(void)createNavigation
{
    self.navigationController.navigationBar.hidden=NO;
    self.navigationController.navigationBar.barTintColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
    //返回
    UIButton *leftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame=CGRectMake(0, 0, 25, 25);
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"left_back"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    
    //标题
    UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
    title.text=@"我的标签";
    title.font=[UIFont systemFontOfSize:22];
    title.textAlignment=NSTextAlignmentCenter;
    title.textColor=[UIColor whiteColor];
    self.navigationItem.titleView=title;
    
    
    //完成
    UIButton *rightBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame=CGRectMake(0, 0, 40, 30);
    rightBtn.titleLabel.font=[UIFont systemFontOfSize:12];
    [rightBtn setTitle:@"完成" forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:rightBtn];
}
//完成
-(void)rightBtnClick:(UIButton *)sender
{
    if (_selectedArray.count==0) {
        [self.view showPromptMessage:YES and:@"最少选中一个标签"];
        _myTimer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerRun) userInfo:nil repeats:YES];
    }else{
        NSArray *arr=[NSArray arrayWithArray:_selectedArray];
        self.returnData(arr);
        self.navigationController.navigationBar.hidden=YES;
        [self.navigationController popViewControllerAnimated:YES];
    }

}
//定时器
static int myTime=0;
-(void)timerRun
{
    if (myTime==2) {
        [_myTimer invalidate];
        _myTimer=nil;
        myTime=0;
        [self.view showPromptMessage:NO and:nil];
    }
    myTime++;
    
}
//返回上一页
-(void)leftBtnClick:(UIButton *)sender
{
    self.navigationController.navigationBar.hidden=YES;
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
